package com.edu.annotation;

import com.edu.core.SpringApplication;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.io.IOException;
import java.net.URL;
import java.util.*;

public class AutoConfigurationImportSelector implements ImportSelector {
  /*  public String[] selectImports(AnnotationMetadata importingClassMetadata) {
       // return new String[]{"modelA.config.AConfig","modelB.config.BConfig"};
        return new String[]{"modelA.config.AConfig"};
    }*/


    private  static  final  String path="META-INF/"
          + "spring-autoconfigure-metadata.properties";

    //@Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        // return new String[]{"modelA.config.AConfig","modelB.config.BConfig"};
        //随便指定一个类加载器
        ClassLoader classLoader = SpringApplication.class.getClassLoader();
        //下面的代码是从spring boot中原样摘抄过来的。
        // 下面的代码其中一个主要的作用就是加载springboot框架自带的starter模块的配置类
        Enumeration<URL> urls = null;
        Properties properties = null;
        try {
            urls = (classLoader != null ? classLoader.getResources(path)
                    : ClassLoader.getSystemResources(path));
             properties = new Properties();

            while (urls.hasMoreElements()) {

                properties.putAll(PropertiesLoaderUtils
                        .loadProperties(new UrlResource(urls.nextElement())));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 这里会找meta-inf目录下的spring.factories文件,找出EnableAutoConfiguration
        //注解关联的类型(loadFactoryNames第二个参数与文件中的要对应起来)
        //主要用来加载第三方的starter的自动配置类
        List<String> candicates = SpringFactoriesLoader.loadFactoryNames(
                EnableAutoConfiguration.class, classLoader);

        List<String> configurations = new ArrayList<String>();

        for (String candicate : candicates) {
            configurations.add(candicate);
        }

        //下面是我随便写的，用来把properties里面的东西拷贝出来.
        //以便把自带starter的配置类与第三方starter的配置类全称合并起来

        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            if (configurations.contains(entry.getKey()) == false) {
                configurations.add(entry.getKey().toString());
            }
        }
        String[] result = new String[configurations.size()];
        configurations.toArray(result);
        return  result;



       /* List<String> configurationClassName = new ArrayList<String>();
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            configurationClassName.add(entry.getKey().toString());
        }
        String[] result = new String[configurationClassName.size()];
        configurationClassName.toArray(result);
        return  result;*/
    }
}
